package com.io.example.demoparent.ES.es;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.io.example.demoparent.ES.entity.User;
import com.io.example.demoparent.ES.util.ESUtil;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/*
    查询文档
 */
public class ES_Doc {

    public static void main(String[] args) throws IOException {
        RestHighLevelClient restHighLevelClient = ESUtil.esRestClient( );
        BatchInsertDoc(restHighLevelClient);
        restHighLevelClient.close();
    }


    /***
     *
     * @param client
     * @throws IOException
     */

    public static void getDoc(RestHighLevelClient client) throws IOException {
        GetRequest getRequest = new GetRequest( );
        getRequest.index("user").id("1001");
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);

        String sourceAsString = response.getSourceAsString( );
        System.out.println("sourceAsString = " + sourceAsString);
    }

    public static void deleteDoc(RestHighLevelClient client) throws IOException {
        DeleteRequest getRequest = new DeleteRequest( );
        getRequest.index("user").id("10050");
        DeleteResponse response = client.delete(getRequest, RequestOptions.DEFAULT);
        DocWriteResponse.Result result = response.getResult( );
        System.out.println("result = " + result);
    }

    /**
     *  批量插入
     * @param client
     * @throws IOException
     */
    public static void BatchInsertDoc(RestHighLevelClient client) throws IOException {
        BulkRequest request = new BulkRequest( );
        ObjectMapper objectMapper = new ObjectMapper( );
        request.add( new IndexRequest( ).index("user").id("10020").source( objectMapper.writeValueAsString(User.builder( ).userName("王五").age(90).gender("女").build( )),XContentType.JSON));
        request.add( new IndexRequest( ).index("user").id("10021").source( objectMapper.writeValueAsString(User.builder( ).userName("李四").age(100).gender("女").build( )),XContentType.JSON));
        request.add( new IndexRequest( ).index("user").id("10022").source( objectMapper.writeValueAsString(User.builder( ).userName("灵力").age(80).gender("男").build( )),XContentType.JSON));
        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);

        TimeValue took = bulk.getTook( );
        BulkItemResponse[] items = bulk.getItems( );
        System.out.println("took = " + took);
        System.out.println("items = " + items);

    }

    /**
     *  批量撒删除
     * @param client
     * @throws IOException
     */
    public static void BatchDeleteDoc(RestHighLevelClient client) throws IOException {
        BulkRequest request = new BulkRequest( );
        ObjectMapper objectMapper = new ObjectMapper( );
        request.add( new DeleteRequest( ).index("user").id("10020"));
        request.add( new DeleteRequest( ).index("user").id("10021"));
        request.add( new DeleteRequest( ).index("user").id("10022"));
        request.add( new DeleteRequest( ).index("user").id("10050"));
        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);

        TimeValue took = bulk.getTook( );
        BulkItemResponse[] items = bulk.getItems( );
        System.out.println("took = " + took);
        System.out.println("items = " + items);


    }

}
